function checkboxToggleWithReason(form, srcChkAllField, srcChkName, desChkAllField, desChkName, beanName, rejChkName, rejReasonName)
{
    desChkAllField.checked = false;
    appRejCheckTickAll(form, srcChkAllField, srcChkName, srcChkName, desChkAllField, desChkName, beanName, rejChkName, rejReasonName)
    appRejSyncRejectReason(form, beanName, rejChkName, rejReasonName);

}
function appRejCheckTickAll(form, srcChkAllField, srcChkName, srcChkField, desChkAllField, desChkName, NOT_USED, beanName, rejChkName, rejReasonName, isClear, isIgnoreDisable)
{
	if( typeof(isClear) == 'undefined' || isClear != false ){
		isClear = true;
	}
	if( typeof(isIgnoreDisable) == 'undefined' || isIgnoreDisable != true ){
		isIgnoreDisable = false;
	}
    if (srcChkField.checked)
    {
        //find index
        var startIndex = srcChkName.indexOf("[");
        var endIndex = srcChkName.indexOf("]");
        var index = srcChkName.substring(startIndex + 1, endIndex);
        var descName = beanName + '[' + index + ']' + '.' + desChkName;
        form.elements[descName].checked = false;
        checkTickAll(form, desChkAllField, desChkName, isIgnoreDisable);
    }
    checkTickAll(form, srcChkAllField, srcChkName, isIgnoreDisable);
    appRejSyncRejectReason(form, beanName, rejChkName, rejReasonName, isClear, isIgnoreDisable);
}
function appRejSyncRejectReason(form, beanName, rejChkName, rejReasonName, isClear, isIgnoreDisable)
{
    if( typeof(isClear) == 'undefined' || isClear != false ){
		isClear = true;
	}
	if( typeof(isIgnoreDisable) == 'undefined' || isIgnoreDisable != true ){
		isIgnoreDisable = false;
	}
    var len = form.elements.length;
    for (var i = 0; i < len; i++){
        try{
            var reasonField = form.elements[beanName + "[" + i + "]." + rejReasonName];
            if(isIgnoreDisable == false && form.elements[beanName + "[" + i + "]." + rejChkName].disabled == true ){ continue; }
            reasonField.readOnly = !form.elements[beanName + "[" + i + "]." + rejChkName].checked;
            setClearReadOnly(reasonField, reasonField.readOnly, isClear);
            if (!reasonField.readOnly) {
                reasonField.focus();
            }
        }catch(e){}
    }
}
//For chk all checkbox to toggle all checkbox
// copy from pti toggle2
function toggleCheckBox(form, fieldAll, chkName) {
    if (!fieldAll) {
        return false;
    }
    tickAll(form, chkName, fieldAll.checked);
}
function checkTickAll(form, fieldAll, chkName, isIgnoreDisable) {
	if( typeof(isIgnoreDisable) == 'undefined' || isIgnoreDisable != true ){
        isIgnoreDisable = false;
    }
    var isCheck = true;
    var startIndex = chkName.indexOf("[");
    var startStr = chkName.substring(0, startIndex + 1);
    var endIndex = chkName.indexOf("]");
    var endStr = chkName.substring(endIndex, chkName.length);

    var inputs = document.getElementsByTagName("INPUT");
    var len = inputs.length;
    for (var i = 0; i <= len; i++) {
        var e = inputs[i];
        if (e) {
            if (e.type) {
                var itemName = e.name;
                if ((e.type == 'checkbox') && (itemName.indexOf(startStr) > -1) && (itemName.indexOf(endStr) > -1) && (!e.disabled || isIgnoreDisable)) {
                    if (e.checked == false) {
                        isCheck = false;
                        break;
                    }
                }
            }
        }
    }
    fieldAll.checked = isCheck;
}
// for 1 checkbox and 1 reason
function checkRejectReason(form, beanName, chkName, reasonName) {
    for (var i = 0; i < form.elements.length; i++)
    {
        try {
            var objCheck = form.elements[beanName + "[" + i + "]." + chkName];
            if (objCheck.checked) {
                var objReason = form.elements[beanName + "[" + i + "]." + reasonName];
                if (trim(objReason.value) == "") {
                    objReason.focus();
                    return false;
                }
            }
        } catch(e) {
        }
    }

    return true;
}
function tickAllWithReason(form, fieldAll, chkName, beanName, rejChkName, rejReasonName, isIgnoreDisable) {
	if( typeof(isIgnoreDisable) == 'undefined' || isIgnoreDisable != true ){
        isIgnoreDisable = false;
    }
    var isCheck;
    if (!fieldAll) {
        return false;
    }

    if (fieldAll.checked == true) {
        isCheck = true;
    } else {
        isCheck = false;
    }
    tickAll(form, chkName, isCheck, isIgnoreDisable);
    appRejSyncRejectReason(form, beanName, rejChkName, rejReasonName, true,isIgnoreDisable);
}
function tickAll(form, chkName, isCheck) {
    var startIndex = chkName.indexOf("[");
    var startStr = chkName.substring(0, startIndex + 1);
    var endIndex = chkName.indexOf("]");
    var endStr = chkName.substring(endIndex, chkName.length);

    var inputs = document.getElementsByTagName("INPUT");
    var len = inputs.length;

    for (var i = len - 1; i >= 0; i--)
    {
        var e = inputs[i];
        if (e) {
            var itemName = e.name;
            if ((e.type == 'checkbox') && (itemName.indexOf(startStr) > -1) && (itemName.indexOf(endStr) > -1) && (e.disabled != true)) {
                e.checked = isCheck;
            }
        }
    }
}
function setClearReadOnly(obj, isReadOnly, isClear){
    if (!obj || typeof(obj) == 'undefined') return false;
    if (typeof(isReadOnly) == 'undefined' || isReadOnly != false) {
        isReadOnly = true;
    }
    if (typeof(isClear) == 'undefined' || isClear != true) {
        isClear = false;
    }
    if (isReadOnly & isClear) {
        obj.value = "";
    }
    var txtStyleClassEnable = 'textboxchar';
    var txtStyleClassDisable = 'textboxcharreadonly';
    try{
	    if(obj.className.indexOf('num') >= 0){
	    	txtStyleClassEnable = 'textboxnum';
	    	txtStyleClassDisable = 'textboxnumreadonly';
	    }
    }catch(e){}
	if(typeof(obj) != 'undefined'){
	    if (!isReadOnly){
	        obj.readOnly = false;
	        if (obj.type == "text") {
	            obj.className = txtStyleClassEnable;
	        } else if (obj.type == "textarea") {
	            obj.className = "textarea";
	        } else if (obj.type == "select-one") {
	            obj.disabled = false;
	        } else if (obj.type == "checkbox") {
	            obj.disabled = false;
	        } else {
	            obj.style.display = '';
	        }
	    }else{
	        obj.readOnly = true;
	        if (obj.type == "text") {
	            obj.className = txtStyleClassDisable;
	        } else if (obj.type == "textarea") {
	            obj.className = "textareareadonly";
	        } else if (obj.type == "select-one") {
	            obj.disabled = true;
	        } else if (obj.type == "checkbox") {
	            obj.disabled = true;
	        } else {
	            obj.style.display = 'none';
	        }
	    }
    }
}
// Function setObjReadOnly(obj, isReadOnly, isClear,className1,className2)
// Param
// obj 			--> Object of form element
// isReadOnly 	--> set readonly flag
// isClear 		--> set for clear the object value
// className1 	--> style when readonly=false
// className2	--> style when readonly=true

function setObjReadOnly(obj, isReadOnly, isClear, className1, className2)
{
    if (obj && obj != 'undefined') {
        if (typeof(isClear) == 'undefined') {
            isClear = true;
        }

        if (!isReadOnly)
        {
            obj.readOnly = false;
            obj.className = className1;
        }
        else
        {
            if (isClear) {
                obj.value = "";
            }
            obj.readOnly = true;
            obj.className = className2;
        }
    }
}

//Function setHiddenObj(obj,isHidden)
//obj		--> Please pass the object to this. by using getElementById.
//isHidden	--> isHidden Flag (default=true)
//isClear	--> isClear Flag (default=true)
function setHiddenObj(obj, isHidden, isClear)
{
    if (obj && obj != 'undefined') {
        if (typeof(isHidden) == 'undefined' || isHidden != false) isHidden = true;
        if (typeof(isClear) == 'undefined' || isClear != false) isClear = true;
        if (isHidden) {
            obj.style.display = 'none';
        } else {
            obj.style.display = '';
        }
        if (isHidden && isClear) {
            obj.value = '';
        }
    }
}
/*
alert all elements in form (for debug)
*/
function alertFormElements(form) {
    if (form && form != 'undefined') {
        var attribute = '+++ [ ' + form.name + ' ] +++\n';
        for (i = 0; i < form.elements.length; i++) {
            attribute = attribute + form.elements[i].name + ' : ' + form.elements[i].value + '  [ ' + form.elements[i].type + ' ] : disable : ' + form.elements[i].disabled + '\n';
        }
        alert(attribute);
    } else {
        alert('form invalid');
    }
}


// get all attributes in object (for debug)
function objToString(obj, description){
	var str = '';
	if( typeof(description) != 'undefined' && description != '' ){
		str = '+++ [ ' + description + ' ] +++\n';
	}
	str += 'typeof - ' + typeof(obj) + '\n';
	if(typeof(obj) != 'undefined'){
		for(key in obj){
			str += key + ' - ' + obj[key] + '\n';
		}
	}
	return str;
}

// alert all attribute in object (for debug)
function describe(obj, description){
	alert(objToString(obj, description));
}

/*
select radio which is match value and clear other radio
radioObj : radio object
value : value to format
*/
function selectRadio(radioObj, value){
	if(isRadioObj(radioObj) && typeof(value) != 'undefined' ){
    	for(var i=0;i<radioObj.length;i++){
			if( radioObj[i].value == value ){
				radioObj[i].checked = true;
			}else{
				radioObj[i].checked = false;
			}
		}
	}else{
		throw new Error('Obj is not radio object');
	}
}
 
/*
map value from resultBean back to jsp field
mapArg : map --> [ [ jspFieldName, resultBeanField ], ... ]
resultBean : resultBeanObj from ws
isFormat : format output or not (default = true)
*/ 
function mapField(mapArg, resultBean, isFormat){
    if( typeof(isFormat) == 'undefined' || isFormat != false ){
        isFormat = true;
    }
	var jspField = '';
	var wsField = '';
	var jspFieldObj;
	
	for( var i=0;i<mapArg.length;i++ ){
		jspField = mapArg[i][0];
		wsField = mapArg[i][1];
		jspFieldObj = eval("form.elements['" + jspField + "']");
		if( typeof(jspFieldObj) != 'undefined' ){
    		if( jspFieldObj.type == 'text' || jspFieldObj.type == 'hidden' ){
	    		//map text
	    		if( isFormat ){
	    			jspFieldObj.value = format( wsField, eval("resultBean." + wsField) );
	    		}else{
	    			if(eval("resultBean." + wsField) == null)
	    			{
	    				jspFieldObj.value = '';
	    			}else
	    			{
	    				jspFieldObj.value = eval("resultBean." + wsField);
	    			}
	    		}
    		}else if( jspFieldObj.type == 'select-one' ){
	    		//map select-one
	    		jspFieldObj.value = eval("resultBean." + wsField);
    		}else if( typeof(jspFieldObj.type) == 'undefined' && typeof(jspFieldObj[0]) != 'undefined' && jspFieldObj[0].type == 'radio' ){
	    		//map radio
	    		selectRadio(jspFieldObj, eval("resultBean." + wsField));
    		}else if( jspFieldObj.type == 'checkbox' ){
	    		//map checkbox
	    		if(jspFieldObj.value == eval("resultBean." + wsField)){
	    			jspFieldObj.checked = true;
	    		}else{
	    			jspFieldObj.checked = false;
	    		}
    		}else{
    			throw new Error(jspField + ' is invalid type !!!');
    		}
		}else{
			throw new Error(jspField + ' is undefined. !!!');
		}
	}
	return true;
}

/*Clear Field
 * form : form with contain fileds to clear
 * mapArg : map --> [ [ jspFieldName, resultBeanField ], ... ]
 * */
function clearField(form, mapArg){
	var jspField = '';
	var jspFieldObj;
    for( var i=0; i<mapArg.length; i++ ){
    	jspField = mapArg[i][0];
		jspFieldObj = eval("form.elements['" + jspField + "']");
        if( typeof(jspFieldObj) != 'undefined' ){
    		if( jspFieldObj.type == 'text' || jspFieldObj.type == 'hidden' ){
	    		//map text
	    		jspFieldObj.value = '';
    		}else if( jspFieldObj.type == 'select-one' ){
	    		//map select-one
	    		jspFieldObj.value = '';
    		}else if( typeof(jspFieldObj.type) == 'undefined' && typeof(jspFieldObj[0]) != 'undefined' && jspFieldObj[0].type == 'radio' ){
	    		//map radio
	    		selectRadio(jspFieldObj, '');
    		}else if( jspFieldObj.type == 'checkbox' ){
	    		//map checkbox
    			jspFieldObj.checked = false;
    		}else{
    			throw new Error(jspField + ' is invalid type !!!');
    		}
		}else{
			throw new Error(jspField + ' is undefined. !!!');
		}
    }
}

	
	function validatorRequired(form,requiredMessage,textboxes,labels){
		trim(textboxes);
		var msg = '';
		var tbox = textboxes.split('$');
		var lb = labels.split('$');
		if (tbox.length) {
			for (var i = 0; i < tbox.length; i++) {
				trim(tbox[i]);
				trim(lb[i]);
				if(form.elements[tbox[i]] != null){
					//Type Check Box
					if(form.elements[tbox[i]].type == 'select-one')
					{
						if(form.elements[tbox[i]].value == '-1'){
							if (msg == '') form.elements[tbox[i]].focus();
							msg = msg + requiredMessage + lb[i] +'\n';
						}
					}else
					{
						if(form.elements[tbox[i]].value == ''){
							if (msg == '') form.elements[tbox[i]].focus();
							msg = msg + requiredMessage + lb[i] +'\n';
						}
					}
				}
			
			}
			if(msg == ''){
				return true;
			}else{
				alert(msg);
				return false;
			}
		}
		return false;
	}
	
	// set disable-enable all list checkBox [true = enable | false = disable]
function setEnableListCheckBox(form, checkObj, isEnable, isClear) {
    if (isClear == undefined) {
        isClear = true;
    }
    if (isEnable == undefined) {
        isEnable = true;
    }
	if(typeof(checkObj) != 'undefined'){
	    var objType = checkObj.type;
	    if (objType == 'checkbox') {
	        var chkName = checkObj.name;
	        var isCheck = false;
	        var startIndex = chkName.lastIndexOf("[");
	        var startStr = chkName.substring(0, startIndex + 1)
	        var endIndex = chkName.lastIndexOf("]");
	        var endStr = chkName.substring(endIndex, chkName.length);
	        var len = form.elements.length;
	        for (var i = 0; i < len; i++) {
	            var e = form.elements[i];
	            if ((e.type == objType) && ((e.name).indexOf(startStr) > -1) && ((e.name).indexOf(endStr) > -1)) {
	                if (!isEnable && isClear) {
	                    e.checked = false;
	                }
	                if (isEnable) {
	                    e.disabled = false;
	                } else {
	                    e.disabled = true;
	                }
	            }
	        }
	    }
    }
}

/*
set all form's elements disable/enable
form : form to be set
isDisable : set disable/enable [ true=disable | false=enable ] (default = true)
isClear : clear value in object ? (default = false)
*/
function setAllFormElementDisable(form, isDisable, isClear) {
    if (form && typeof(form) != 'undefined') {
        if (typeof(isDisable) == 'undefined' || isDisable != false) {
            isDisable = true;
        }
        if (typeof(isClear) == 'undefined' || isClear != true) {
            isClear = false;
        }
        var len = form.elements.length;
        for (var i = len - 1; i >= 0; i--)
        {
            var element = form.elements[i];
            element.disabled = isDisable;
            if (isDisable && isClear) {
                element.value = '';
            }
        }
    }
}

//if at lease one is checked == return true
function checkTickOne(form, chkName, objType) {
    if (typeof(objType) == 'undefined') {
        objType = 'checkbox';
    }
    var isCheck = false;
    var startIndex = chkName.indexOf("[");
    var startStr = chkName.substring(0, startIndex + 1)
    var endIndex = chkName.indexOf("]");
    var endStr = chkName.substring(endIndex, chkName.length);

    var len = form.elements.length;
    for (var i = 0; i < len; i++) {
        var e = form.elements[i];
        if ((e.type == objType) && ((e.name).indexOf(startStr) > -1) && ((e.name).indexOf(endStr) > -1)) {
            if (e.checked) {
                isCheck = true;
                break;
            }
        }
    }
    return isCheck;
}

function textCounter(field, maxlimit) {
		if (field.value.length > maxlimit) // if too long...trim it!
			field.value = field.value.substring(0, maxlimit);
	}
	
function telephone_onkeypress() {
    var allow_key_Pattern = /[ 0-9\-]/i;

    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    var character = String.fromCharCode(code);

    if (allow_key_Pattern.test(character)) {
        return true;
    } else {
        return false;
    }
}

function telephone_onkeyup(item) {
    return true;
}

function telephone_onmouseout(item) {
    var telephone_Pattern = /^([0-9][ 0-9\-]*)$/i;
    if (telephone_Pattern.test(item.value)) {
        window.status = "";
        return true;
    } else {
        if (item.value != "") {
            window.status = "Wrong Format";
        }
        item.value = "";
        return false;
    }
}

function telephone_onblur(item) {
    var telephone_Pattern = /^([0-9][ 0-9\-]*)$/i;
    if (telephone_Pattern.test(item.value)) {
        window.status = "";
        return true;
    } else {
        if (item.value != "") {
            window.status = "Wrong Format";
        }
        item.value = "";
        return false;
    }
}